package GUI;

import javax.swing.JOptionPane;

import Kernel.Administrateur;
import Kernel.Avion;
import Kernel.Bateau;
import Kernel.Bus;
import Kernel.Train;
import Kernel.VehiculeGenerique;

public class AjoutVehicule extends javax.swing.JFrame {

	private static final long serialVersionUID = 61757198587051670L;

	/** Cree une nouvelle instance de AjoutVehicule */
	public AjoutVehicule() {
		initComponents();
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Initialisation des composants d'AWT.
	 */
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		capacite = new javax.swing.JTextField();
		vitesse = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jComboBox1 = new javax.swing.JComboBox();
		ajouter = new javax.swing.JButton();
		retour = new javax.swing.JButton();
		jLabel6 = new javax.swing.JLabel();
		referenceVehicule = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("Ajouter un vehicule");

		jLabel2.setText("km/h");

		jLabel3.setText("personnes");

		jLabel4.setText("Capacite :");

		jLabel5.setText("Vitesse :");

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Avion", "Bateau", "Bus", "Train", "Autre" }));

		ajouter.setText("Ajouter");
		ajouter.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ajouterActionPerformed(evt);
			}
		});

		retour.setText("Retour");
		retour.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				retourActionPerformed(evt);
			}
		});

		jLabel6.setText("Reference :");

		jLabel7.setText("(Par defaut si vide)");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		retour)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		290,
																		Short.MAX_VALUE)
																.addComponent(
																		ajouter))
												.addComponent(
														jComboBox1,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														152,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap())
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel5)
												.addComponent(jLabel4)
												.addComponent(jLabel6))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(referenceVehicule)
												.addComponent(
														vitesse,
														javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														capacite,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														129, Short.MAX_VALUE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel2)
												.addComponent(jLabel3)
												.addComponent(jLabel7))
								.addGap(63, 63, 63))
				.addGroup(
						layout.createSequentialGroup().addGap(139, 139, 139)
								.addComponent(jLabel1)
								.addContainerGap(165, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addGap(13, 13, 13)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel6)
												.addComponent(
														referenceVehicule,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel7))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														capacite,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel4))
								.addGap(10, 10, 10)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														vitesse,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel5))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										48, Short.MAX_VALUE)
								.addComponent(jComboBox1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(39, 39, 39)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(ajouter)
												.addComponent(retour))
								.addContainerGap()));

		pack();
	}

	private void retourActionPerformed(java.awt.event.ActionEvent evt) {
		Administration at = new Administration(new Administrateur(Administration.admin.getLogin(), Administration.admin.getPassword(), Accueil.serveur));
		at.setVisible(true);
		this.setVisible(false);
		this.dispose();
	}

	private void ajouterActionPerformed(java.awt.event.ActionEvent evt) {
		if (this.capacite.getText().compareTo("") == 0
				|| this.vitesse.getText().compareTo("") == 0) {
			JOptionPane.showMessageDialog(this,
					"Les champs ne peuvent etre vide !", "Pas glop...",
					JOptionPane.ERROR_MESSAGE);
		}
		if (this.jComboBox1.getSelectedItem().toString().compareTo("Avion") == 0) {
			if (this.referenceVehicule.getText().compareTo("") == 0) {
				Accueil.serveur.ajouterVehicule(
						new Avion(Integer.parseInt(this.capacite.getText()),
								Integer.parseInt(this.vitesse.getText())));
			} else {
				if (!Accueil.serveur.existeDejaVehicule(
						new Avion(this.referenceVehicule.getText(), Integer
								.parseInt(this.capacite.getText()), Integer
								.parseInt(this.vitesse.getText()))))
					Accueil.serveur.ajouterVehicule(
							new Avion(this.referenceVehicule.getText(), Integer
									.parseInt(this.capacite.getText()), Integer
									.parseInt(this.vitesse.getText())));
				else
					JOptionPane.showMessageDialog(this,
							"Le vehicule existe deja !", "Pas glop...",
							JOptionPane.ERROR_MESSAGE);
			}
		} else if (this.jComboBox1.getSelectedItem().toString()
				.compareTo("Bateau") == 0) {
			if (this.referenceVehicule.getText().compareTo("") == 0) {
				Accueil.serveur.ajouterVehicule(
						new Bateau(Integer.parseInt(this.capacite.getText()),
								Integer.parseInt(this.vitesse.getText())));
			} else {
				if (!Accueil.serveur.existeDejaVehicule(
						new Bateau(this.referenceVehicule.getText(), Integer
								.parseInt(this.capacite.getText()), Integer
								.parseInt(this.vitesse.getText()))))
					Accueil.serveur.ajouterVehicule(
							new Bateau(this.referenceVehicule.getText(),
									Integer.parseInt(this.capacite.getText()),
									Integer.parseInt(this.vitesse.getText())));
				else
					JOptionPane.showMessageDialog(this,
							"Le vehicule existe deja !", "Pas glop...",
							JOptionPane.ERROR_MESSAGE);
			}

		} else if (this.jComboBox1.getSelectedItem().toString()
				.compareTo("Bus") == 0) {
			if (this.referenceVehicule.getText().compareTo("") == 0) {
				Accueil.serveur.ajouterVehicule(
						new Bus(Integer.parseInt(this.capacite.getText()),
								Integer.parseInt(this.vitesse.getText())));
			} else {
				if (!Accueil.serveur.existeDejaVehicule(
						new Bus(this.referenceVehicule.getText(), Integer
								.parseInt(this.capacite.getText()), Integer
								.parseInt(this.vitesse.getText()))))
					Accueil.serveur.ajouterVehicule(
							new Bus(this.referenceVehicule.getText(), Integer
									.parseInt(this.capacite.getText()), Integer
									.parseInt(this.vitesse.getText())));
				else
					JOptionPane.showMessageDialog(this,
							"Le vehicule existe deja !", "Pas glop...",
							JOptionPane.ERROR_MESSAGE);
			}
		} else if (this.jComboBox1.getSelectedItem().toString()
				.compareTo("Train") == 0) {
			if (this.referenceVehicule.getText().compareTo("") == 0) {
				Accueil.serveur.ajouterVehicule(
						new Train(Integer.parseInt(this.capacite.getText()),
								Integer.parseInt(this.vitesse.getText())));
			} else {
				if (!Accueil.serveur.existeDejaVehicule(
						new Train(this.referenceVehicule.getText(), Integer
								.parseInt(this.capacite.getText()), Integer
								.parseInt(this.vitesse.getText()))))
					Accueil.serveur.ajouterVehicule(
							new Train(this.referenceVehicule.getText(), Integer
									.parseInt(this.capacite.getText()), Integer
									.parseInt(this.vitesse.getText())));
				else
					JOptionPane.showMessageDialog(this,
							"Le vehicule existe deja !", "Pas glop...",
							JOptionPane.ERROR_MESSAGE);
			}

		} else if (this.jComboBox1.getSelectedItem().toString()
				.compareTo("Autre") == 0) {
			Accueil.serveur.ajouterVehicule(
					new VehiculeGenerique(this.referenceVehicule.getText(),
							Integer.parseInt(this.capacite.getText()), Integer
									.parseInt(this.vitesse.getText())));

		}
		Administration ad = new Administration(new Administrateur(Administration.admin.getLogin(), Administration.admin.getPassword(), Accueil.serveur));
		this.setVisible(false);
		ad.setVisible(true);
		this.dispose();
	}

	private javax.swing.JButton ajouter;
	private javax.swing.JTextField capacite;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JTextField referenceVehicule;
	private javax.swing.JButton retour;
	private javax.swing.JTextField vitesse;
}
